<?php
namespace app\mec\controller;
use  think\Db;
use think\Exception;

class Notice extends Base {

    /**
     * 消息列表
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['a.n_title|um.mechanism_name'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.n_createtime'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.n_createtime'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.n_createtime'] = ['between time',[$start,$end]];
            }
            $map['a.n_status'] = 1;
            $map['a.n_mechanism'] = session('mmid');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('notice')->alias('a')
                ->join('up_mechanism um','um.mechanism_id = a.n_mechanism','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::name('notice')->alias('a')
                ->join('up_mechanism um','um.mechanism_id = a.n_mechanism','left')
                ->field('a.*,um.mechanism_name')
                ->order('a.n_id desc')
                ->where($map)
                ->page($page, $limit)
                ->select();//计算总页面

            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function delete(){
        $ids = input('param.ids');
        Db::startTrans();// 启动事务
        $res =  Db::table('up_notice')->where('n_id',$ids)->setField('n_status',2);
        if($res){
            Db::commit();
            writelog(session('uid'),session('username'),'删除成功',1);
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            writelog(session('uid'),session('username'),'删除失败',2);
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data['n_title'] = input('param.title');
            $data['n_range'] = input('param.cate_id');//发布范围
            $data['n_type'] = 1;//消息类型
            if(input('param.cate_id')==1){
                $type_name = input('param.type_name');
                $user_id = Db::table('up_u_user')->where('user_to_mechanism_id',$type_name)->column('user_id');
                $data['n_user_id'] =','.implode(',',$user_id).',';
            }else{
                $data['n_user_id'] = ','.implode(',',input('param.user_id/a')).',';
            }
            $data['n_mechanism'] = 1;
            $data['n_createtime'] =date('Y-m-d H:i:s',time());
            $data['n_detail'] =input('param.detail');
            $data['n_content'] =input('param.content');
            $data['n_read_user'] =',0,';
            $data['n_clear_user'] =',0,';
            $data['n_img'] = input('param.images');

            $aaa = Db::table('up_notice')->insertGetId($data);
            if($aaa){
                $list = [];
                $JP_uid = explode(',', trim($data['n_user_id'], ','));
                foreach($JP_uid as $k=>$v){
                    $va = (string)$v;
                    array_push($list, $va);
                }
                try{
                    $this->Jpush(1, $list, '系统消息', ['type'=>1], $data['n_title'], 1);
                }catch(\Exception $e){
                    
                }
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $whereMap['a.user_to_mechanism_id'] = array('IN',$this->mechanism_id);
        $list = Db::table('up_u_user')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.user_to_mechanism_id','left')
            ->field('a.user_id,a.user_username,um.mechanism_name')
            ->where($whereMap)
            ->select();
        $whereMap1['mechanism_id'] = array('IN',$this->mechanism_id);

        $mechanism = Db::table('up_mechanism')
                            ->where('mechanism_status',2)
                            ->where($whereMap1)
                            ->field('mechanism_id,mechanism_name')
                            ->select();
        $this->assign('mechanism',$mechanism);
        $this->assign('list',$list);
        return $this->fetch();
    }


}